package com.example.iqiyitest.view;

import com.example.iqiyitest.R;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.RelativeLayout;


/**
 * 电影分类Button 获取焦点变大
 * @author zhong
 *
 */
public class FocusBiggerRel extends RelativeLayout {
	private Context mContext;
	private ObjectAnimator animX;
	private ObjectAnimator animY;
	
	public FocusBiggerRel(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		
		this.setFocusable(true);
		this.setFocusableInTouchMode(true);
//		this.setBackgroundResource(R.drawable.second_item_bg);
	}

	public FocusBiggerRel(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public FocusBiggerRel(Context context) {
		this(context, null);
	}
	
	@Override
	protected void onFocusChanged(boolean focused, int direction,
			Rect previouslyFocusedRect) {
		super.onFocusChanged(focused, direction, previouslyFocusedRect);

		if (focused) {
//			second_item_select_bg
			this.setBackgroundResource(R.drawable.second_menu_bg);
			startAnim();
		} else {
			soptAnim();
			this.setBackgroundColor(Color.TRANSPARENT);
		}
	}
	
	@SuppressLint("NewApi")
	public void startAnim(){
		animX = ObjectAnimator.ofFloat(this, "scaleX", 1.00f);
		animY = ObjectAnimator.ofFloat(this, "scaleY", 1.00f);
		AnimatorSet set = new AnimatorSet();
		set.playTogether(animX,animY);
		set.setDuration(300);
//		set.setInterpolator(new BounceInterpolator());
		set.start();
		set.addListener(new AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animator animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animator animation) {
			}
			
			@Override
			public void onAnimationCancel(Animator animation) {
				
			}
		});
	}
	
	@SuppressLint("NewApi")
	public void soptAnim(){
		animX = ObjectAnimator.ofFloat(this, "scaleX", 1.0f);
		animY = ObjectAnimator.ofFloat(this, "scaleY", 1.0f);
		AnimatorSet set = new AnimatorSet();
		set.playTogether(animX,animY);
		set.setDuration(300);
//		set.setInterpolator(new BounceInterpolator());
		set.start();
		set.addListener(new AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animator animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animator animation) {
			}
			
			@Override
			public void onAnimationCancel(Animator animation) {
				
			}
		});
	}
}
